/* 
 * File:   main.cpp
 * Author: Guan Junming <junming.guan@kisters.cn>
 *
 * Created on March 29, 2010, 4:11 PM
 */

#include <stdlib.h>
#include <cstdlib>
#include <list>
#include <iostream>
#include "overload.h"


using namespace std;

bool number_exists(int player_number, list<int>& pool);
bool is_valid_play_number(int player_number, int& last_number);
bool is_game_over(int last_number, list<int> pool);
void print_pool(list<int> pool);
void change_pools(int play_number, list<int>& pool, list<int>& out_pool);
int count_out_pool(list<int> out_pool);
void helloworld(string hello = "hello");

int main(int argc, char** argv) {

    string world = "world";
    string hello = "hello";
    helloworld();    

    int game_number, player_number = 0, last_number;
    list<int> out_pool, pool;
    bool is_continued = false;

    do {
        cout << "please input a number for game:" << endl;
        cin >> game_number;
        for (int i = 1; i <= game_number; i++) {
            pool.push_back(i);
        }
        print_pool(pool);

        cout << "Now game begin!!!" << endl;
        last_number = rand() % game_number; //here RAND_MAX is set to game_number;
        cout << "The first number is " << last_number << endl;
        change_pools(last_number, pool, out_pool);

        //do cycle to get a player_number and check it;


        while (!is_game_over(last_number, pool)) {

            while (!number_exists(player_number, pool)) {
                cout << "Please select your number:";
                cin >> player_number;
            }

            //check_number(player_number,)
            if (is_valid_play_number(player_number, last_number)) {
                pool.remove(player_number);
                out_pool.push_back(player_number);
                last_number = player_number;
                cout << "Yeah, Great You Got it, Continue!" << endl;
                cout << "There are " << pool.size() << " numbers left!" << endl;
                cout << "Last Number is :" << last_number << endl;
                print_pool(pool);
            } else {
                cout << "Ah, Your lost it:" << endl;
                player_number = 0;
            }
        }
        cout << "This game is over, your score is :" << count_out_pool(out_pool) << endl;
        cout << "Do you want try again:";
        cin >> is_continued;

    } while (is_continued);


    return (EXIT_SUCCESS);
}

void helloworld(string hello) {
    cout << hello;
}

bool number_exists(int player_number, list<int>& pool) {
    list<int>::iterator lt, end = pool.end();
    for (lt = pool.begin(); lt != end; lt++) {
        if (player_number == *lt) {
            cout << *lt;
            return true;
        }
    }

    return false;
}

bool is_valid_play_number(int player_number, int& last_number) {
    if (player_number % last_number == 0 || last_number % player_number == 0) {
        return true;
    }
    return false;
}

void print_pool(list<int> pool) {
    list<int>::iterator lt, end = pool.end();
    for (lt = pool.begin(); lt != end; lt++) {
        cout << *lt << " ";
    }
    cout << endl;
}

bool is_game_over(int last_number, list<int> pool) {
    if (pool.size() > 0) {
        list<int>::iterator lt, end = pool.end();
        for (lt = pool.begin(); lt != end; lt++) {
            if (is_valid_play_number(*lt, last_number)) {
                return false;
            }
        }
    }
    return true;
}

void change_pools(int play_number, list<int>& pool, list<int>& out_pool) {
    pool.remove(play_number);
    out_pool.push_back(play_number);
}

int count_out_pool(list<int> out_pool) {
    list<int>::iterator lt, end = out_pool.end();
    int summary_out_pool = 0;
    for (lt = out_pool.begin(); lt != end; lt++) {
        summary_out_pool += *lt;
    }

    return summary_out_pool;
}